package com.fenqiyong.www;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wangnan.library.GestureLockThumbnailView;
import com.wangnan.library.GestureLockView;
import com.wangnan.library.listener.OnGestureLockListener;
import com.wangnan.library.painter.System360Painter;

import ren.nearby.http.base.BaseActivity;
import ren.nearby.http.utils.util.Colors;
import ren.nearby.http.utils.util.PreferencesUtils;

/**
 * Created by Administrator on 2019/3/12.
 */

public class LockAct extends BaseActivity implements OnGestureLockListener, View.OnClickListener {
    GestureLockView mGestureLockView;
    GestureLockThumbnailView mGestureLockThumbnailView;
    TextView tv_pwd;
    String mResult;
    int count = 5;
    TextView tv;
    boolean reset = false;

    String result1 = "";


    @Override
    public void onIntent() {
        mResult = getIntent().getStringExtra("result");
    }

    @Override
    public int getContentView() {
        return R.layout.lock;
    }

    @Override
    public void initView() {
        builder
                .setIconLeft(R.drawable.ic_arrow_left)
                .setBack(true)
                .setTitle("设置手势锁")
                .setBackgroundColor(R.drawable.shape_status_color2)
                .setTitleColor(R.color.rent_title_color)
                .setStatusBarTintResource(R.drawable.shape_status_color1)
                .build();
        tv_pwd = findViewById(R.id.tv_pwd);
        tv = findViewById(R.id.tv);
        tv.setOnClickListener(this);
        if (TextUtils.isEmpty(mResult)) {
            tv_pwd.setText("绘制解锁图案");
        } else {
            reset = true;
            tv_pwd.setText("请输入原手势密码");
            tv.setVisibility(View.GONE);
        }
        mGestureLockThumbnailView = findViewById(R.id.gltv);
        mGestureLockView = findViewById(R.id.glv);
        mGestureLockView.setPainter(new System360Painter());
        mGestureLockView.setGestureLockListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv) {
            tv_pwd.setText("绘制解锁图案");
            result1 = "";
            tv_pwd.setTextColor(getResources().getColor(R.color.rent_sub_color));
            PreferencesUtils.putString(this, "result", "");
        }
    }

    /**
     * 解锁开始监听方法
     */
    @Override
    public void onStarted() {
        count--;
        if (count == 0 && reset) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setTitle("手势密码已失效");
            alertDialog.setMessage("请重新登录");
            alertDialog.setPositiveButton("手机验证", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    PreferencesUtils.putString(getApplicationContext(), "result", "");
                    dialog.dismiss();
                    startActivity(new Intent(getApplicationContext(), ALoginAct.class));
                    finish();
                }
            });
            alertDialog.show();
        }
    }

    /**
     * 解锁密码改变监听方法
     */
    @Override
    public void onProgress(String progress) {
        Logger.e("当前密码: " + progress);

        mGestureLockThumbnailView.setThumbnailView(progress, 0xFF1E8BDE);
    }

    /**
     * 解锁完成监听方法
     */
    @Override
    public void onComplete(String result) {
        if (TextUtils.isEmpty(result)) {
            return;
        }
        if (!mResult.equals("")) {
            if (!mResult.equals(result) && count > 0) {
                mGestureLockView.showErrorStatus(400);
                tv_pwd.setText("密码错误，还可输入" + count + "次");
            } else if(mResult.equals(result)){
                PreferencesUtils.putString(this, "result", result);
//                startActivity(new Intent(this, SHAct.class));
                finish();
            }
            return;
        }
        if (result1.equals("")) {
            result1 = result;
            tv.setVisibility(View.INVISIBLE);
            tv_pwd.setText("再次绘制解锁图案");
            mGestureLockView.clearView();
            return;
        }
        if (!result1.equals(result)) {
            mGestureLockView.showErrorStatus(400);
            mGestureLockView.clearView();
            tv_pwd.setText("与上次输入不一致，请重新输入");
            tv_pwd.setTextColor(Colors.RED);
            tv.setVisibility(View.VISIBLE);
        } else {
            tv_pwd.setText("输入密码正确");
            tv_pwd.setTextColor(getResources().getColor(R.color.rent_sub_color));
            PreferencesUtils.putString(this, "result", result);
//            startActivity(new Intent(this, SHAct.class));
            finish();

        }


    }
}

